package com.zdzx.service.impl;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Base64Utils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import com.zdzx.common.result.Result;
import com.zdzx.common.result.ResultCode;
import com.zdzx.common.result.ResultUtil;
import com.zdzx.mapper.InfoMapper;
import com.zdzx.model.constant.SystemConfig;
import com.zdzx.model.service.file.Info;
import com.zdzx.service.OSSService;
import com.zdzx.utils.MinioUtil;

import io.minio.PutObjectOptions;

/**
 * <p>
 * oss
 * </p>
 *
 * @author szl
 * @since 2018-11-08
 */
@Service
public class OssServiceImpl implements OSSService{
	
	@Autowired
	private InfoMapper infoMapper;
	@Override
	public Result<String> upload(String bucketName, String type, MultipartFile file) {
		try {
			InputStream inputStream = file.getInputStream();
			int lastIndexOf = file.getOriginalFilename().lastIndexOf(".");
			String substring = file.getOriginalFilename().substring(lastIndexOf);
			PutObjectOptions options = new PutObjectOptions(inputStream.available(), -1);
			options.setContentType(file.getContentType());
			String filePath = MinioUtil.uploadFile(bucketName, type+"/"+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMM")) + "/" + UUID.randomUUID() + substring, inputStream,
					options);
			if (!StringUtils.isEmpty(filePath)) {
				Info entity=new Info();
				entity.setBucketName(bucketName);
				entity.setFilePath(filePath);
				entity.setFileName(file.getOriginalFilename());
				entity.setCreateTime(LocalDateTime.now());
				entity.setFileType(type);
				infoMapper.insert(entity);
				return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, SystemConfig.ossIp+ filePath);
			}
			} catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), null, null);
		}
		return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), null, null);
	}

	@Override
	public Result<String> removeFile(String url) {
		try {
			url=new String(Base64Utils.decode(url.getBytes()),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, null);
		}
		String replace = url.replace(SystemConfig.ossIp, "");
		int indexOf = replace.indexOf("/");
		String bucketName=replace.substring(0, indexOf);
		String objectName=replace.substring(indexOf);
		System.out.println(bucketName+"--------------"+objectName);
		boolean remove = MinioUtil.remove(bucketName, objectName);
		return ResultUtil.data(remove ? ResultCode.RESULT_SUCCESS.getCode() : ResultCode.RESULT_ERROR.getCode(), null,
				null);
	}

	@Override
	public Result<String> getUrl(String bucketName, String objectName) {
		try {
			String expiryUrl = MinioUtil.getExpiryUrl(bucketName, objectName.substring(1,objectName.length()));
			if (StringUtils.isEmpty(expiryUrl)) {
				return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), null, null);
			}
			else {
				expiryUrl = expiryUrl.replace(SystemConfig.ossIpDoMain, SystemConfig.ossIp.substring(0,SystemConfig.ossIp.length()-1));
				return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, expiryUrl);
			}
			} catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), null, null);
		}
	}
	
	
	@Override
	public InputStream downFile(String bucketName, String objectName) {
		try {
			 InputStream downFile = MinioUtil.downFile(bucketName, objectName);
			 System.out.println(downFile.available());
			return downFile;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	
	}

	public static void main(String[] args) {
		System.out.println(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMM")));
	}

}
